﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace TUM.Mustertexte_Parser
{
    public class TemplateElement
    {
        public readonly TemplateElement[] ChildElements;

        public TemplateElement ParentElement
        {
            get;
            private set;
        }


        internal TemplateElement(TemplateElement[] childElements)
        {
            ChildElements = childElements;

            if (childElements != null)
            {
                foreach (var curChild in childElements)
                    curChild.ParentElement = this;
            }
        }
    }

    public class TemplateParagraph : TemplateElement
    {
        internal TemplateParagraph(TemplateElement[] contentElements)
            : base(contentElements)
        {
        }
    }

    public class TemplateTable : TemplateElement
    {
        public readonly TemplateElement[,] Cells;

        public readonly int Columns;

        public readonly int Rows;

        internal TemplateTable(TemplateTableRow[] tableRows)
            : base(tableRows)
        {
            //Get row/column cound
            Rows = tableRows.Length;
            Columns = 0;
            foreach (TemplateTableRow curRow in tableRows)
            {
                if (curRow.ChildElements.Length > Columns)
                    Columns = curRow.ChildElements.Length;
            }

            //Fill cells
            Cells = new TemplateElement[Columns, Rows];
            for(int y = 0; y < Rows; y++)
            {
                TemplateTableRow curRow = tableRows[y];
                for (int x = 0; x < curRow.ChildElements.Length; x++)
                    Cells[x, y] = curRow.ChildElements[x];
            }
        }
    }

    public class TemplateTableRow : TemplateElement
    {
        internal TemplateTableRow(TemplateTableCell[] columns)
            : base(columns)
        {
        }
    }

    public class TemplateTableCell : TemplateElement
    {
        internal TemplateTableCell(TemplateElement content)
            : base(content.GetType().Equals(typeof(TemplateElement)) ? content.ChildElements : new[] { content }) //If container -> children , else -> the element
        {
        }
    }

    public class TemplateList : TemplateElement
    {
        internal TemplateList(TemplateElement[] listItems)
            : base(listItems)
        {
        }
    }

    public class TemplateBulletList : TemplateList
    {
        internal TemplateBulletList(TemplateElement[] listItems)
            : base(listItems)
        {
        }
    }

    public class TemplateNumberedList : TemplateList
    {
        internal TemplateNumberedList(TemplateElement[] listItems)
            : base(listItems)
        {
        }
    }

    public class TemplateListItem : TemplateElement
    {
        internal TemplateListItem(TemplateElement[] contentElements)
            : base(contentElements)
        {
        }
    }

    public class TemplateBold : TemplateElement
    {
        internal TemplateBold(TemplateElement[] boldItems)
            : base(boldItems)
        {
        }
    }

    public class TemplateItalic : TemplateElement
    {
        internal TemplateItalic(TemplateElement[] italicItems)
            : base(italicItems)
        {
        }
    }

    public class TemplateText : TemplateElement
    {
        public readonly string Text;

        internal TemplateText(string text)
            : base(new TemplateElement[]{ })
        {
            if (text == null) throw new ArgumentNullException("text");
            Text = text;
        }
    }

    public class TemplateImage : TemplateElement
    {
        public readonly string SourceFile;

        public readonly int Width;

        public readonly int Height;

        internal TemplateImage(string sourceFile)
            : base(new TemplateElement[] { })
        {
            if (sourceFile == null) throw new ArgumentNullException("sourceFile");
            SourceFile = sourceFile;

            try
            {
                using (Image img = Image.FromFile(sourceFile))
                {
                    Width = img.Width;
                    Height = img.Height;
                }
            }
            catch { }
        }

        internal TemplateImage(string sourceFile, int width, int height)
            : base(new TemplateElement[] { })
        {
            if (sourceFile == null) throw new ArgumentNullException("sourceFile");
            SourceFile = sourceFile;

            Width = width;
            Height = height;
        }
    }
}
